package com.pan.api.format.impl.markdown;


import com.pan.api.entity.ArgumentInformation;
import com.pan.api.format.ArgumentFormat;
import com.pan.api.utils.PanJsonConvertUtil;

import java.util.function.BiFunction;

//这个模板的替换关键词貌似怎么写也和代码耦合在一起，貌似暂时也没啥好的处理办法
//算了要耦合就彻底一点
//这里将该类与模板一起装起来，
//对外暴露一个转换接口就行了
//对外依赖ParameterStandardization，
public final class MarkdownArgumentFormat
        extends ArgumentFormat
        implements PanJsonConvertUtil {
    String argumentTemplate;


    private final BiFunction<String, String, String> replaceParameterName = (template, pn) -> template.replace("{parameterName}", pn);
    private final BiFunction<String, String, String> replaceComment = (template, cn) -> template.replace("{comment}", cn);
    private final BiFunction<String, String, String> replaceRequestType = (template, rt) -> template.replace("{requestType}", rt);
    private final BiFunction<String, String, String> replaceJson = (template, js) -> template.replace("{json}", js);

    {//paramName, comment, requestType, json
        argumentTemplate =
                """
                        
                      **Argument** : {paramName}
                      - comment: {comment}                             
                      - request type: {requestType}  
                      - argument form:   
                      ```json  
                      {json}
                      ```
                """;
        /**
         * 还是不要将模板写在配置文件里比较好
         */
        String regex = "\\{\\w+?\\}";
        initialization(argumentTemplate,regex);
    }
    @Override
    public final String convert(ArgumentInformation tar) {
        System.out.println(tar);
        String requestType = tar.getParamType().toString();
        String paramName = tar.getParamName();
        String comment = tar.getComment();
        Class paramClass = tar.getParamClass();
        String json = convertToJson(paramClass);
        json = json.strip().replaceAll("\n","\n        ");
        String result = replace(paramName, comment, requestType, json);
        return result;
    }
}
